/*

    This file is part of Hometree.

    Hometree is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Hometree is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with Hometree.  If not, see <http://www.gnu.org/licenses/>.

    Copyright 2011,2012 Roman Held

*/

/********************************************************************\
                          Includes
\********************************************************************/
#include "htrsmonitor.h"
#include "htrsmap.h"
#include "htrsmimedataresource.h"
#include "htr/htrglobal.h"

/********************************************************************\
                          Implementation
\********************************************************************/

/*--------------------------------------------------------------------
*/
HtrsMonitor::HtrsMonitor(HtrsMap *map)
 : QObject()
{
  Q_ASSERT(map);
  mMap = map;
  connect(mMap, SIGNAL(created(HtrsResource *)),
          this, SLOT(resourceCreated(HtrsResource *)),
          Qt::QueuedConnection);
  connect(mMap, SIGNAL(changed(HtrsResource *)),
          this, SLOT(resourceChanged(HtrsResource *)),
          Qt::QueuedConnection);
  connect(mMap, SIGNAL(aboutToDelete(HtrsResource *)),
          this, SLOT(resourceAboutToDelete(HtrsResource *)));
}

/*--------------------------------------------------------------------
*/
HtrsMonitor::~HtrsMonitor()
{
}

/*--------------------------------------------------------------------
*/
void HtrsMonitor::sendIdents()
{
  QList<HtrsResource*> list = mMap->resources();
  foreach(HtrsResource *r, list)
    resourceCreated(r);
}

/*--------------------------------------------------------------------
*/
void HtrsMonitor::resourceCreated(HtrsResource *res)
{
  Q_ASSERT(res);

  // TODO: gleicher Code auch in HtrsResource.. evt. auslagern?
  QByteArray  outPaket;
  QDataStream outStream(&outPaket,QIODevice::WriteOnly);
  HtrManagedSocket::setupStream(outStream);
  outStream << (qint32)Htr::Net::ClIdentMsg;
  outStream << res->url();
  outStream << (qint32)res->id();
  outStream << (qint32)res->type();
  emit sendMsg(outPaket);
}

/*--------------------------------------------------------------------
*/
void HtrsMonitor::resourceChanged(HtrsResource *res)
{
  Q_ASSERT(res);

  QByteArray  outPaket;
  QDataStream outStream(&outPaket,QIODevice::WriteOnly);
  HtrManagedSocket::setupStream(outStream);
  outStream << (qint32)Htr::Net::ClMonitorMsg;
  outStream << (qint32)res->id();
  outStream << (qint32)Htr::Net::EventMsg;
  outStream << (qint32)Htr::Net::EvChanged;
  emit sendMsg(outPaket);
}

/*--------------------------------------------------------------------
*/
void HtrsMonitor::resourceAboutToDelete(HtrsResource *res)
{
  Q_ASSERT(res);

  QByteArray  outPaket;
  QDataStream outStream(&outPaket,QIODevice::WriteOnly);
  HtrManagedSocket::setupStream(outStream);
  outStream << (qint32)Htr::Net::ClMonitorMsg;
  outStream << (qint32)res->id();
  outStream << (qint32)Htr::Net::EventMsg;
  outStream << (qint32)Htr::Net::EvRemoved;
  emit sendMsg(outPaket);
}


